const crypto = require('crypto');
const $config = require('../config');
const MongoClient = require('mongodb').MongoClient;

module.exports = {
  md5: async (data) => {
    return crypto.createHash('md5').update(data).digest('hex');
  },
  findMany: async (searchData, project, sort={}) => { //查询、过滤、排序
    const client = await MongoClient.connect($config.url, {useNewUrlParser:true});
    const dbo = client.db($config.collections);
    const resultData = await dbo.collection($config.documents).find(searchData)
        .project(project)
        .sort(sort)
        .toArray();
    client.close();
    return resultData;
  },
  count: async (searchData) => {
    const client = await MongoClient.connect($config.url, {useNewUrlParser:true});
    const dbo = client.db($config.collections);
    const resultData = await dbo.collection($config.documents).find(searchData).count()
    return resultData
  },
  find: async (searchData, project, skip=0, limit=1, sort={}) => { //查询、过滤、跨度、回复数、排序
    const client = await MongoClient.connect($config.url, {useNewUrlParser:true});
    const dbo = client.db($config.collections);
    const resultData = await dbo.collection($config.documents).find(searchData)
        .project(project)
        .skip(parseInt(skip))
        .limit(parseInt(limit))
        .sort(sort)
        .toArray();
    client.close();
    return resultData;
  },
  insertOne: async (insrtStr) => {
    const client = await MongoClient.connect($config.url, {useNewUrlParser:true});
    const dbo = await client.db($config.collections);
    const resultData = await dbo.collection($config.documents).insertOne(insrtStr);
    client.close();
    return resultData
  },
  insertMany: async (arrayStr) => {
    const client = await MongoClient.connect($config.url, {useNewUrlParser:true});
    const dbo = await client.db($config.collections);
    const resultData = await dbo.collection($config.documents).insertMany(arrayStr);
    client.close();
    return resultData
  },
  updateOne: async (whereStr, updateStr) => {
    const client = await MongoClient.connect($config.url, {useNewUrlParser:true});
    const dbo = await client.db($config.collections);
    const resultData = await dbo.collection($config.documents).updateOne(whereStr, {$set: updateStr});
    client.close();
    return resultData
  },
  updateMany: async (whereStr, updateStr) => {
    const client = await MongoClient.connect($config.url, {useNewUrlParser:true});
    const dbo = await client.db($config.collections);
    const resultData = await dbo.collection($config.documents).updateMany(whereStr, {$set: updateStr});
    client.close();
    return resultData
  },
  deleteOne: async (deleteOne) => {
    const client = await MongoClient.connect($config.url, {useNewUrlParser:true});
    const dbo = await client.db($config.collections);
    const resultData = await dbo.collection($config.documents).deleteOne(deleteOne);
    client.close();
    return resultData
  },
  deleteMany: async (deleteMany) => {
    const client = await MongoClient.connect($config.url, {useNewUrlParser:true});
    const dbo = await client.db($config.collections);
    const resultData = await dbo.collection($config.documents).deleteMany(deleteMany);
    client.close();
    return resultData
  }
};
